<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Reg extends MY_Controller {

	function index(){
        $this->load->model('User_model');
        $post_data = $this->input->post();
        if($post_data){
            $has_error = 0;
            if(empty($post_data['phone'])){
                $this->smarty->assign("phone_err", 1);
                $has_error = 1;
            }else
            {
                $this->smarty->assign("phone", $post_data['phone']);
                //已经填写手机号、验证用户是否存在

                $user = $this->User_model->get_user_by_phone($post_data['phone']);
                if(empty($user)){
                    //用户不存在，验证密码是否填写
                    if(empty($post_data['password'])){
                        $this->smarty->assign("msg", '密码不能为空');
                        $has_error = 1;
                    }
                    if(empty($post_data['con_password'])){
                        $this->smarty->assign("msg", '确认密码不能为空');
                        $has_error = 1;
                    }
                    if(!empty($post_data['password']) && !empty($post_data['con_password']) && $post_data['con_password'] != $post_data['password']){
                        $this->smarty->assign("msg", '两次密码输入不一致');
                        $has_error = 1;
                    }
                    $this->smarty->assign("password_has", 0);
                }else $this->smarty->assign("password_has", 1);
            }
            if(empty($post_data['vcode'])){
                $this->smarty->assign("msg", '验证码不能为空');
                $has_error = 1;
            }

            //判断验证码是否正确
            if(empty($post_data['phone'])==false && empty($post_data['vcode'])==false){
                //查询用户最后一条code
                $this->load->model("Verification_code");
                $code_model = $this->Verification_code->getLastCodeByPhone($post_data['phone'],1);
                if($code_model['is_use']==1||$code_model['code']!=$post_data['vcode']){
                    //验证码错误
                    $this->smarty->assign("msg", '验证码错误');
                    $has_error = 1;
                }
            }
            
            if($has_error == 0){
				$md5_pass = md5($post_data['password']);
				$rand_str = '';
				for($i=0; $i<10; $i++){
					$rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
				}

				$password = md5($md5_pass.$rand_str);
				$register_ip = $_SERVER["REMOTE_ADDR"];

                //添加注册
                $result = $this->User_model->reg_user($post_data['phone'],$rand_str,$password,$register_ip);
                if($result > 0) {
                    // reg_wecenter($post_data['phone'], $post_data['password'], $post_data['phone']."@qq.com");
                    header("Location:/login");exit;
                }
            }

        }
        $this->smarty->display("reg.html");
    }


    /**
     * 获取手机验证码
     * @param $phone
     */
    function reg_msg($phone){

         // $send_result = send($phone,'1234');

         // print_r($send_result);exit;

        //查询手机用户
        $this->load->model('User_model');
        $user = $this->User_model->get_user_by_phone($phone);
        if(empty($user)==false){
                print_r('{"code":300,"msg":"您已经注册过了，请登录"}');
                exit ;
        }

        //查询用户最后一条code
        $this->load->model("Verification_code");
        $code_model = $this->Verification_code->getLastCodeByPhone($phone,1);
        if(empty($code_model) || strtotime($code_model['create_on'])+1*60<time()){
            $code = getRandChar(4);
            $result = $this->Verification_code->insertCodeLog($phone,$code,1);
            if($result>0){
                $send_result = send_verify_code_msg($phone,$code);
                if(empty($user)){
                    //未注册，未开店
                    // echo 1;
                    print_r($send_result);exit;
                }else{
                    //已注册、未开店
//                    print_r(json_decode($send_result));
                    if(json_decode($send_result)->code==200){
                        print_r('{"code":201,"msg":"已注册、未开店"}');exit;
                    }else{
                        print_r($send_result);exit;
                    }
                }
                return ;
            }else{
                print_r('{"code":400,"msg":"系统异常"}');
                return ;
            }
        }else{
            print_r('{"code":500,"msg":"您操作太频率了！请稍后"}');
            return ;
        }

    }


    /**
     * 注册iframe
     * @return [type] [description]
     */
    public function iframe(){
        $post_data = $this->input->post();

        if($post_data){
            if(empty($post_data['phone'])){
                print_r('{"result":"-1","msg":"手机号不能为空"}');exit;
            }
            if(empty($post_data['vcode'])){
                print_r('{"result":"-1","msg":"验证码不能为空"}');exit;
            }
            if(empty($post_data['pwd'])){
                print_r('{"result":"-1","msg":"密码不能为空"}');exit;
            }
            if(empty($post_data['pwd'])){
                print_r('{"result":"-1","msg":"密码不能为空"}');exit;
            }


            //查询手机用户
            $this->load->model('User_model');
            $user = $this->User_model->get_user_by_phone($post_data['phone']);
            if(empty($user)==false){
                    
                    print_r('{"result":"-2","msg":"您已经注册过了，请登录"}');exit;
            }

            //查询用户最后一条code
            $this->load->model("Verification_code");
            $code_model = $this->Verification_code->getLastCodeByPhone($post_data['phone'],1);
            if($code_model['is_use']==1||$code_model['code']!=$post_data['vcode']){
                //验证码错误
                print_r('{"result":"-1","msg":"验证码错误"}');exit;
            }
            
            //开始注册
            $md5_pass = md5($post_data['pwd']);
            $rand_str = '';
            for($i=0; $i<10; $i++){
                $rand_str .= substr($md5_pass, rand(0, strlen($md5_pass)-1), 1);
            }

            $password = md5($md5_pass.$rand_str);
            $register_ip = $_SERVER["REMOTE_ADDR"];

            //添加注册
            $result = $this->User_model->reg_user($post_data['phone'],$rand_str,$password,$register_ip);
            if($result > 0) {
                //设置cookie
                $user_data = array(
                    'id' => $result,
                    'head_image' => '',
                    'user_name' => '',
                    'mobile' => $post_data['phone'],
                    'logged_in' => 1);
                $this->session->set_userdata('user_data', $user_data);
                print_r('{"result":"0","msg":"success"}');exit;
            }else{
                print_r('{"result":"-1","msg":"系统异常"}');exit;
            }
            

        }




        
        $this->smarty->display("reg_iframe.html");
    }
}
